/**
 * 修改密码
 * Created by KY on 2017/4/17.
 */
define(['ldfw/core', 'art', 'text!tpl/user/change_pwd.tpl',
        'weui', 'comps/ajax', 'comps/session', 'ldfw/ldlibs/md5', 'ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW, artTemplate, tpl, weui, ajax, session) {
        var View = LDFW.Container.extend({
            tpl: tpl,
            className:'panel changePwd-panel',
            initialize: function () {
                this.intervalKey = 'changePwd-delay-key'
            },
            rendered: function () {
                var sendSmgDelay = session.getItem(this.intervalKey)
                if(sendSmgDelay){
                    this.disableSendCode()
                    this.$el.find('#sendCode').html('重新发送('+sendSmgDelay+')');
                    this.startInterval(sendSmgDelay)
                }
            },
            events: {
                'click #forgotOldPwd': 'forgotOldPwdHandler',
                'click #pwdChange': 'pwdChangeHandler',
                'click #changePwd': 'changePwd',
                'click #resetPwd': 'resetPwd',
                'click #sendCode':'sendCode'
            },
            changePwd: function () {
                let _this = this,
                    currentUser = session.getCurrentUser()
                weui.form.validate('#changePwdForm', function (error) {
                    if (error) {
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur', function () {
                            $(this).removeClass('error')
                        })
                    } else {
                        let oldPwd = $('#old_cust_password').val(),
                            newPwd = '',
                            newCustPassword = $("#changePwdForm input[name=new_cust_password]").val(),
                            repetCustPassword = $("#changePwdForm input[name=repet_cust_password]").val()
                        if (newCustPassword !== repetCustPassword) {
                            $.alert('两次密码输入不一致')
                            return
                        }
                        oldPwd = hex_md5(currentUser.cust_loginname+'#'+oldPwd)
                        newPwd = hex_md5(currentUser.cust_loginname+'#'+newCustPassword)
                        ajax.post('pfL.5.101', {
                            cust_loginname:currentUser.cust_loginname,
                            old_cust_passwd:oldPwd,
                            cust_passwd: newPwd
                        }).success(function (resp) {
                            if(resp.resultCode){
                                $.alert(resp.resultMsg)
                            }else{
                                $.alert('修改密码成功',function() {
                                    LDFW.navigate('main')
                                })
                            }
                        })
                    }
                })
            },
            resetPwd: function () {
                let _this = this,
                    currentUser = session.getCurrentUser()
                weui.form.validate('#resetPwdForm', function (error) {
                    if (error) {
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur', function () {
                            $(this).removeClass('error')
                        })
                    } else {
                        let newCustPassword = $("#resetPwdForm input[name=new_cust_password]").val(),
                            repetCustPassword = $("#resetPwdForm input[name=repet_cust_password]").val(),
                            validCode = _this.$el.find('#valid_code').val()
                        if (newCustPassword !== repetCustPassword) {
                            $.alert('两次密码输入不一致')
                            return
                        }
                        newCustPassword = hex_md5(currentUser.cust_loginname+'#'+newCustPassword)
                        let data = {
                            cust_loginname:currentUser.cust_loginname,
                            phone:currentUser.phone,
                            cust_passwd: newCustPassword,
                            check_code: validCode
                        }
                        ajax.post('pfL.5.103', data).success(function (resp) {
                            if(resp.resultCode){
                                $.alert(resp.resultMsg)
                                return
                            }
                            $.alert('重置密码成功',function(){
                                LDFW.navigate('main')
                            })
                        })
                    }
                })
                return false
            },

            forgotOldPwdHandler: function (e) {
                var $link = $(e.target);
                $('#resetPwdForm').show()
                $('#changePwdForm').hide()
                return false;
            },
            pwdChangeHandler: function (e) {
                var $link = $(e.target);
                $('#changePwdForm').show()
                $('#resetPwdForm').hide()
                return false;
            },
            startInterval:function(delay){
                const _this = this;
                this.disableSendCode()
                let interval = setInterval(function(){
                    if(delay >= 0){
                        session.setItem(_this.intervalKey,delay)
                        _this.$el.find('#sendCode').html('重新发送('+delay+')')
                        delay--
                    }else{
                        _this.removeInterval(interval)
                    }
                },1000)
            },
            removeInterval:function(interval){
                clearInterval(interval)
                session.removeItem(this.intervalKey)
                this.$el.find('#sendCode').html('获取验证码')
                this.enableSendCode()
            },
            sendCode:function() {
                const _this = this
                let currentUser = session.getCurrentUser(),
                    number = 60,
                    interval = null,
                    phone = currentUser.phone
                if(phone){
                    ajax.get('sendResetLoginPassWordCheckCode.do',{phone_no:phone})
                    .success(function(resp){
                        if(resp.resultCode){
                            $.alert(resp.resultMsg,function(){
                                _this.enableSendCode()
                            })
                        }else{
                            phone = phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
                            $.toptip('验证码已成功发送到手机'+phone+'上','success')
                            _this.startInterval(60)
                        }
                    })
                    .error(function(){
                        _this.removeInterval(interval)
                    })
                }else{
                    $.toptip('您尚未绑定手机号码,请使用原密码修改','warning')
                }

            },

            disableSendCode:function(){
                var $sendCodeBtn = this.$el.find('#sendCode')
                $sendCodeBtn.addClass('weui-btn_disabled')
                $sendCodeBtn.prop('disabled',true)
            },
            enableSendCode:function(){
                var $sendCodeBtn = this.$el.find('#sendCode')
                $sendCodeBtn.removeClass('weui-btn_disabled')
                $sendCodeBtn.removeAttr('disabled')
            }
        })
        return new View()
    }
)
